﻿using System;
using System.Collections.Generic;
using System.Text;

namespace skydream
{
    /// <summary>
    /// 数据校验服务
    /// </summary>
    public class Transform
    {
        //
        // 摘要:
        //     字符串转化为字节数组 16；
        //    
        //      
        // 返回结果:
        //     返回串口接受的数据byte【】
        public static byte[] StringToByte(string InString)
        {
            string[] ByteStrings = new string[InString.Length / 2];
            for (int i = 0; i < InString.Length; i += 2)
            {
                ByteStrings[i / 2] = InString.Substring(i, 2);

            }
            byte[] ByteOut;
            ByteOut = new byte[ByteStrings.Length];
            for (int i = 0; i < ByteStrings.Length; i++)
            {
                ByteOut[i] = Convert.ToByte(ByteStrings[i], 16);

            }
            return ByteOut;
        }
        /// <summary>
        /// byte转16进制字符；
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ByteToHexString(byte[] bytes) // 0xae00cf => "AE00CF "
        {
            string hexString = string.Empty;
            if (bytes != null)
            {
                StringBuilder strB = new StringBuilder();

                for (int i = 0; i < bytes.Length; i++)
                {
                    strB.Append(bytes[i].ToString("X2"));
                }
                hexString = strB.ToString();
            }
            return hexString;
        }

        /// <summary>
        /// 10进制字符串转16进制字符串；
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string tenStrToHexStr(string ten) // 0xae00cf => "AE00CF "
        {
            string hexString = string.Empty;
            StringBuilder strB = new StringBuilder();
            for (int i = 0; i < ten.Length / 2; i++)
            {
                string temp=int.Parse(ten.Substring(i * 2, 2)).ToString("X2");
                strB.Append(temp);
            }
            hexString = strB.ToString();
            return hexString;
        }
        /// <summary>
        /// 异或校验，字符要求是10进制的字符
        /// 通过字符串转化为带校验码的字符串（字符，其实校验，终止校验）
        /// 1.10进制字符转为16进制给byte
        /// 2.byte进行校验然后交给byte
        /// </summary>比如aa0222    aa022220   从第一位1,校验2位
        /// <param name="str">从第几位开始校验，校验几位</param>
        /// <param name="firstCheck"></param>
        /// <param name="lastCheck"></param>
        /// <returns></returns>
        public static string getXorCheckSum(string str, int firstCheck, int lastCheck)
        {
            int strLen = str.Length / 2;//计算str共有几位，
            byte[] bt = new byte[strLen];
            byte temp = 0x00;
            //赋值
            for (int i = 0; i < strLen; i++)
            {
                bt[i] = Convert.ToByte(Convert.ToInt32(str.Substring(i * 2, 2), 16));//从第i位开始取
            }
            //开始校验
            temp = bt[firstCheck];
            for (int i = firstCheck; i <= lastCheck - firstCheck; i++)
            {

                temp = Convert.ToByte(temp ^ bt[(i + 1)]);
            }
            str = str + Convert.ToString(int.Parse(temp.ToString()), 16).PadLeft(2,'0');
            return str;
        }
        /// <summary>
        /// 异或校验，字符要求是10进制的字符
        /// 通过字符串转化为带校验码的字符串（字符，其实校验，终止校验）
        /// 1.10进制字符转为16进制给byte
        /// 2.byte进行校验然后交给byte
        /// </summary>
        /// <param name="str"></param>
        /// <param name="firstCheck"></param>
        /// <param name="lastCheck"></param>
        /// <returns></returns>
        public static string getXorCheck(string str, int firstCheck, int lastCheck)
        {
            int strLen = str.Length / 2;//计算str共有几位，
            byte[] bt = new byte[strLen];
            byte temp = 0x00;
            //赋值
            for (int i = 0; i < strLen; i++)
            {
                bt[i] = Convert.ToByte(Convert.ToInt32(str.Substring(i * 2, 2), 16));//从第i位开始取
            }
            //开始校验
            temp = bt[firstCheck];
            for (int i = firstCheck; i <= lastCheck - firstCheck; i++)
            {

                temp = Convert.ToByte(temp ^ bt[(i + 1)]);
            }
            str =Convert.ToString(int.Parse(temp.ToString()), 16).PadLeft(2, '0');
            return str;
        }
        /// <summary>
        /// 10进制2，8，16进制转换
        /// </summary>
        /// <returns></returns>
        public static string getTenToStr(int ten,int Ary)
        {
            string temp=null;
            //十进制转二进制
            //十进制转八进制
            //十进制转十六进制
            temp=Convert.ToString(ten, Ary);
            return temp;
        }
        /// <summary>
        /// 2，8，16进制转换10进制
        /// </summary>
        /// <returns></returns>
        public static int getStrToTen(string trans, int Ary)
        {
            int temp = 0;
            //十进制转二进制
            //十进制转八进制
            //十进制转十六进制
            temp = Convert.ToInt32(trans, Ary);
            return temp;
        }

    }
}
